<?php
class Default_View_Filter_CheckAppoint 
{
	private $_error;
	private $_messagesError;
	public function __construct($data)
	{
		$this->_error = false;
		$this->checkNotChooseCustomer($data);
		if($this->isValid())
		{
			$this->checkAllDetails($data);
		}
		if($this->isValid())
		{
			$this->checkLocalData($data);
		}
		if($this->isValid())
		{
			$this->checkInDb($data);
		}
		if(!$this->isValid())
		{
//			if (!isset($this->_messagesError['henry']))
//				$this->_messagesError['henry']="<b><i>If have any error plase contact CHAN HENRY</i></b>";
		}
	}
	private function checkInDb($data)
	{
		$appointment_Id = $data['appoint_id'];
		$data = $data['data'];
		$objAppDetail = new Default_Model_AppointDetails();
		$empOb = new Default_Model_Employee();
		$rowCount = 0;
		foreach ($data as $item)
		{
			if ($item['employee'] == Default_Model_Employee::SPECAL_EP) continue;			
			$rowCount++;
			$dataInDB = $objAppDetail->getRowByEmployee($item['employee']);
			//Kiem tra moi phan tu trong DB co hop ly khong
			if (count($dataInDB)<=0) continue;		
			$localFlag = false;	
			foreach ($dataInDB as $itemInDb)
			{
				if ($appointment_Id == $itemInDb['appointment_id']) continue;
				$startTimeInDB = new Zend_Date($itemInDb['start_time']) ;	
				$endTimeInDB   = new Zend_Date($itemInDb['end_time']) ;
				if (
					(	$item['start_time']->get()>$startTimeInDB->get()
					 &&	$item['start_time']->get()<$endTimeInDB->get()
					)
					||
					( 
						$item['end_time']->get()>$startTimeInDB->get()
					&&	$item['end_time']->get()<$endTimeInDB->get()
				    )
				    ||
					( 
						$item['start_time']->get()<=$startTimeInDB->get()
					&&	$item['end_time']->get()>=$endTimeInDB->get()
				    )
				   )
			    {
			    	$empName = $empOb->getName($item['employee']);
			    	$this->_messagesError[] = "Error in No.$rowCount $empName is busy at TIME: ". $startTimeInDB->get('H:m')." to ".$endTimeInDB->get('H:m');
			    	$this->_error = true;
			    	$localFlag = true;
			    }
			}	
		}
	}
	private function checkAllDetails($data)
	{
		$empOb = new Default_Model_Employee();
		$data = $data['data'];
		$rowCount = 0;
		foreach ($data as $item)
		{
			$rowCount++;
			if ((int)$item['employee']==-1)
			{
				$this->_error=true;
				$this->_messagesError[] = "Error in No.$rowCount Not choose employee in Detail Jobs. You must choose Employee";
			}
			if ((int)$item['services']==-1)
			{
				$this->_error=true;
				$this->_messagesError[] = "Error in No.$rowCount You must choose Services for ".$empOb->getName($item['employee']);
			}
			$startTime = $item['start_time'];
			$endTime   = $item['end_time'];
			if ($endTime->get()<=$startTime->get())
			{
				$this->_error=true;
				$this->_messagesError[] = "Error in No.$rowCount ".$empOb->getName($item['employee']). " Start Time have less than End Time";				
			}
		}
	}
	private function checkNotChooseCustomer($data)
	{
		if ((int)$data['appoint_customer']==-1)
		{
			$this->_error=true;
			$this->_messagesError[] = "Not choose customer. You must choose a customer";
		}
	}
	private function checkLocalData($data)
	{
		$empOb = new Default_Model_Employee();
		$serOb = new Default_Model_Services();
		$data = $data['data'];
		$nItem = count($data);
		if ($nItem>1)
		{
			for ($i=0;$i<$nItem-1;$i++)
			{
				if ($data[$i]['employee'] == Default_Model_Employee::SPECAL_EP) continue;
				for ($j=$i+1;$j<$nItem;$j++)
				{		
					if ($data[$i]['employee']!=$data[$j]['employee']) continue;
					if(!$this->checkDate($data[$i]['start_time'],$data[$i]['end_time'],$data[$j]['start_time'],$data[$j]['end_time']))
					{
						$this->_messagesError[] = "Error in No.".($i+1)." ".$empOb->getName($data[$i]['employee'])." for ".$serOb->getName($data[$i]['services'])." time not correct with No.".($j+1)." for service ".$serOb->getName($data[$j]['services']);
						$this->_error =true;	
					}
				}
			}
		}
	} 
	private function checkDate($Start1,$End1,$Start2,$End2)
	{
		$Start1 = $Start1->get();
		$End1   = $End1->get();
		$Start2 = $Start2->get();
		$End2   = $End2->get();
		
		if ($Start1>$Start2 && $Start1 < $End2) return false;
		if ($End1>$Start2 && $End1 < $End2) return false;
		return true;
	}
	public static function checkOneItemForAjax($data)
	{
		/*$data = array(
    		'employee'   =>  $params['empId'],
    		'services'   =>  $params['serId'],
    		'start_time' =>  $params['startTime'],
    		'end_time'   =>  $params['endTime']
    	);*/
		$startDate = new Zend_Date($data['start_time']);
		$endDate   = new Zend_Date($data['end_time']);
		$result = array();
		if ($data['employee'] == -1)
		{
			$result['error'] = "false";
			$result['message'][] = "Plase choose Employee";
		}
		if ($data['services'] == -1)
		{
			$result['error'] = "false";
			$result['message'][] = "Plase choose Sevices";
		}
		if ($startDate->get()>=$endDate->get())
		{
			$result['error'] = "false";
			$result['message'][] = "End date greater than start date";
		}
		if (isset($result['error'])) 
		{
			$result['message'][] = "<b>Error in previous line</b>";
			return $result;
		}
		//Check in DB;
		$objAppDetail = new Default_Model_AppointDetails();
		$empOb = new Default_Model_Employee();
		$dataInDb = $objAppDetail->getRowByEmployee($data['employee']);
		if (count($dataInDb)>0)
		{
			foreach ($dataInDb as $itemInDb)
			{
				$startTimeInDB = new Zend_Date($itemInDb['start_time']) ;	
				$endTimeInDB   = new Zend_Date($itemInDb['end_time']) ;
				if (
					(	$startDate->get()+1>$startTimeInDB->get()
					 &&	$startDate->get()-1<$endTimeInDB->get()
					)
					||
					( 
						$endDate->get()+1>$startTimeInDB->get()
					&&	$endDate->get()-1<$endTimeInDB->get()
				    )
				    ||
					( 
						$startDate->get()-1<$startTimeInDB->get()
					&&	$endDate->get()+1>$endTimeInDB->get()
				    )
				   )
			    {
			    	$empName = $empOb->getName($data['employee']);
			    	$result['message'][] = "$empName is busy at TIME: ". $startTimeInDB->get('H:m')." to ".$endTimeInDB->get('H:m');
			    	$result['error'] = "false";
			    }
			}
		}	
		if ($result['error'] == "false")
    	{
			$result['message'][] = "<b>Error in previous line</b>";
    	}
		return $result;
	}
	public function isValid()
	{
		return !$this->_error;
	}
	public function getMessages()
	{
		return $this->_messagesError;
	}
}

